package com.blockmeta.bbs.businesslibrary.pojo;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

import buf.TickerOuterClass;

/**
 * Created by author on 2017/12/27.
 */

public class CoinsDetailPOJO {

    /**
     * ticker : {"coin":"btc","convert_cny":6.576,"currency":"usd","exchange":"coinbase","fee":1.0E-4,"key":"coinbase","name":"Coinbase","ticker":{"buy":"15670.21","date":"1514374170000","high":"16490.0","last":"15670.21","low":"15100.01","price_24h_before":"15142.61","sell":"15670.22","volume":"22347.58557284"},"url":"https://www.coinbase.com/"}
     */

    private TickerBeanX ticker;

    public TickerBeanX getTicker() {
        return ticker;
    }

    public void setTicker(TickerBeanX ticker) {
        this.ticker = ticker;
    }

    public CoinsDetailPOJO(TickerOuterClass.Ticker pTicker) {
        update(pTicker);
    }

    private void update(TickerOuterClass.Ticker pTicker) {
        ticker = parseTicker(pTicker);
    }

    private TickerBeanX parseTicker(TickerOuterClass.Ticker pTicker) {
        ticker = new TickerBeanX();

        ticker.setConvert_cny(pTicker.getConvertCny());
        int index = pTicker.getPair().indexOf("_");
        if(index > 0) {
            ticker.setCoin(pTicker.getPair().substring(0,index));
            String currency = pTicker.getPair().substring(index + 1);
            ticker.setCurrency(currency);
            ticker.setCurrencyShow(currency);
        }
        else
        {
            ticker.setCoin(pTicker.getPair());
            ticker.setCurrency(pTicker.getPair());
            ticker.setCurrencyShow(pTicker.getPair());
        }
        ticker.setCurrencyShow(ticker.getCurrencyShow());
        ticker.setExchange(pTicker.getExchange());
        ticker.setKey(ticker.exchange);
        ticker.setName(pTicker.getName());
        ticker.setFee(Double.parseDouble(pTicker.getFee()));
        TickerBeanX.TickerBean tickerBean = new TickerBeanX.TickerBean();
        TickerOuterClass.TickerItem item = pTicker.getTicker();
        if(item == null)
            return null;
        tickerBean.setBuy(String.valueOf(item.getBuy()));
        tickerBean.setDate(String.valueOf(item.getDate()));
        tickerBean.setHigh(String.valueOf(item.getHigh()));
        tickerBean.setLast(String.valueOf(item.getLast()));
        tickerBean.setLow(String.valueOf(item.getLow()));
        tickerBean.setPrice_24h_before(String.valueOf(item.getPrice24HBefore()));
        tickerBean.setSell(String.valueOf(item.getSell()));
        tickerBean.setVolume(String.valueOf(item.getVolume()));
        ticker.setTicker(tickerBean);
        return ticker;
    }

    public static class TickerBeanX implements Parcelable {
        public int getOptional_valid() {
            return optional_valid;
        }

        public void setOptional_valid(int optional_valid) {
            this.optional_valid = optional_valid;
        }

        /**

         * coin : btc
         * convert_cny : 6.576  汇率
         * currency : usd
         * exchange : coinbase
         * fee : 1.0E-4
         * key : coinbase
         * name : Coinbase
         * ticker : {"buy":"15670.21","date":"1514374170000","high":"16490.0","last":"15670.21","low":"15100.01","price_24h_before":"15142.61","sell":"15670.22","volume":"22347.58557284"}
         * url : https://www.coinbase.com/
         */

        private String coin;
        private double convert_cny;
        private String currency;
        @SerializedName("currency_show")
        private String currencyShow;
        private String exchange;
        private double fee;
        private String key;
        private String name;
        private TickerBean ticker;
        private String url;
        private int optional_valid; //0  未选择   1 自选
        public int warning_valid;//0 未设置预警或者预警都已经触发 1 有未触发的预警
        @SerializedName("coin_key")
        public String coinKey;
        private String logo;

        public String getLogo() {
            return logo;
        }

        public void setLogo(String logo) {
            this.logo = logo;
        }

        public String getCoin() {
            return coin;
        }

        public void setCoin(String coin) {
            this.coin = coin;
        }

        public double getConvert_cny() {
            return convert_cny;
        }

        public void setConvert_cny(double convert_cny) {
            this.convert_cny = convert_cny;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getCurrencyShow() {
            return currencyShow;
        }

        public void setCurrencyShow(String currencyShow) {
            if (currencyShow.contains("nextweek")){
                this.currencyShow = currencyShow.replace("nextweek","次周合约");
            }else if (currencyShow.contains("swaps")){
                this.currencyShow = currencyShow.replace("swaps","永续合约");
            }else if (currencyShow.contains("thisquarter")){
                this.currencyShow = currencyShow.replace("thisquarter","季度合约");
            }else if (currencyShow.contains("thisweek")){
                this.currencyShow = currencyShow.replace("thisweek","当周合约");
            }else {
                this.currencyShow = currencyShow;
            }
        }

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public double getFee() {
            return fee;
        }

        public void setFee(double fee) {
            this.fee = fee;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public TickerBean getTicker() {
            return ticker;
        }

        public void setTicker(TickerBean ticker) {
            this.ticker = ticker;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public static class TickerBean implements Parcelable {
            /**
             * buy : 15670.21
             * date : 1514374170000
             * high : 16490.0
             * last : 15670.21
             * low : 15100.01
             * price_24h_before : 15142.61
             * sell : 15670.22
             * volume : 22347.58557284
             */

            private String buy;
            private String date;
            private String high;
            private String last;
            private String low;
            private String price_24h_before;
            private String sell;
            private String volume;

            public String getBuy() {
                return buy;
            }

            public void setBuy(String buy) {
                this.buy = buy;
            }

            public String getDate() {
                return date;
            }

            public void setDate(String date) {
                this.date = date;
            }

            public String getHigh() {
                return high;
            }

            public void setHigh(String high) {
                this.high = high;
            }

            public String getLast() {
                return last;
            }

            public void setLast(String last) {
                this.last = last;
            }

            public String getLow() {
                return low;
            }

            public void setLow(String low) {
                this.low = low;
            }

            public String getPrice_24h_before() {
                return price_24h_before;
            }

            public void setPrice_24h_before(String price_24h_before) {
                this.price_24h_before = price_24h_before;
            }

            public String getSell() {
                return sell;
            }

            public void setSell(String sell) {
                this.sell = sell;
            }

            public String getVolume() {
                return volume;
            }

            public void setVolume(String volume) {
                this.volume = volume;
            }

            @Override
            public int describeContents() {
                return 0;
            }

            @Override
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeString(this.buy);
                dest.writeString(this.date);
                dest.writeString(this.high);
                dest.writeString(this.last);
                dest.writeString(this.low);
                dest.writeString(this.price_24h_before);
                dest.writeString(this.sell);
                dest.writeString(this.volume);
            }

            public TickerBean() {
            }

            protected TickerBean(Parcel in) {
                this.buy = in.readString();
                this.date = in.readString();
                this.high = in.readString();
                this.last = in.readString();
                this.low = in.readString();
                this.price_24h_before = in.readString();
                this.sell = in.readString();
                this.volume = in.readString();
            }

            public static final Creator<TickerBean> CREATOR = new Creator<TickerBean>() {
                @Override
                public TickerBean createFromParcel(Parcel source) {
                    return new TickerBean(source);
                }

                @Override
                public TickerBean[] newArray(int size) {
                    return new TickerBean[size];
                }
            };
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.coin);
            dest.writeDouble(this.convert_cny);
            dest.writeString(this.currency);
            dest.writeString(this.currencyShow);
            dest.writeString(this.exchange);
            dest.writeDouble(this.fee);
            dest.writeString(this.key);
            dest.writeString(this.name);
            dest.writeParcelable(this.ticker, flags);
            dest.writeString(this.url);
            dest.writeInt(this.optional_valid);
            dest.writeString(this.coinKey);
        }

        public TickerBeanX() {
        }

        protected TickerBeanX(Parcel in) {
            this.coin = in.readString();
            this.convert_cny = in.readDouble();
            this.currency = in.readString();
            this.currencyShow = in.readString();
            this.exchange = in.readString();
            this.fee = in.readDouble();
            this.key = in.readString();
            this.name = in.readString();
            this.ticker = in.readParcelable(TickerBean.class.getClassLoader());
            this.url = in.readString();
            this.optional_valid = in.readInt();
            this.coinKey = in.readString();
        }

        public static final Creator<TickerBeanX> CREATOR = new Creator<TickerBeanX>() {
            @Override
            public TickerBeanX createFromParcel(Parcel source) {
                return new TickerBeanX(source);
            }

            @Override
            public TickerBeanX[] newArray(int size) {
                return new TickerBeanX[size];
            }
        };
    }
}
